package com.seadragon.app.ksr.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.seadragon.app.ksr.model.Customer;
import com.seadragon.app.ksr.model.KsrConstants;
import com.seadragon.app.ksr.model.Office;
import com.seadragon.app.ksr.model.Review;
import com.seadragon.app.ksr.service.CustomerService;
import com.seadragon.app.ksr.service.OfficeService;
import com.seadragon.app.ksr.service.ReviewService;
import com.seadragon.app.ksr.util.CookieUtil;

@Controller
public class EntityController {

	@Autowired
	private CustomerService customerService;

	@Autowired
	private OfficeService officeService;

	@Autowired
	private ReviewService reviewService;
	
   @RequestMapping("/business.html")
	public String showBusiness(Model model,@RequestParam(value = "id", required = true) String id){
	   model.addAttribute("business", DummyData.getDummyBusiness());
		return "business";
	}
   
   @RequestMapping("/office.html")
   public String showOffice(HttpServletRequest request, HttpServletResponse response,Model model,@RequestParam(value = "id", required = true) String id){
	   if (StringUtils.isNotEmpty(id)){
		   Office office =officeService.findById(new Long(id));
		   if (office!=null){
			   model.addAttribute("office", office);
			   model.addAttribute("reviewsFor5str", reviewService.findByOfficeAndReview(office, 100));
			   model.addAttribute("reviewsFor4str", reviewService.findByOfficeAndReview(office, 80));
			   model.addAttribute("reviewsFor3str", reviewService.findByOfficeAndReview(office, 60));
			   model.addAttribute("reviewsFor2str", reviewService.findByOfficeAndReview(office, 40));
			   model.addAttribute("reviewsFor1str", reviewService.findByOfficeAndReview(office, 20));
			   model.addAttribute("reviewList", reviewService.findByOffice(office));
			   //save cookie
			   try {
					CookieUtil.saveCookie(Long.toString(office.getId()), "office", response, request.getCookies(), request.getContextPath());
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		   }
	   }
//	   model.addAttribute("office", DummyData.getDummyOffice());
//	   model.addAttribute("centerLocation", DummyData.getDummyOffice().getAddress());
//	   model.addAttribute("reviewList", DummyData.getReviewList());
	   return "customer.office";
   }
   
   @RequestMapping("/reviewer.html")
   public String showReviewer(HttpServletRequest request, HttpServletResponse response,Model model,@RequestParam(value = "id", required = true) String id){
	   if (StringUtils.isNotEmpty(id)){
		   Customer reviewer =customerService.findById(new Integer(id));
		   if (reviewer!=null){
			   model.addAttribute("reviewer", reviewer);
			   model.addAttribute("reviewsFor5str", reviewService.findByCustomerAndReview(reviewer, 100));
			   model.addAttribute("reviewsFor4str", reviewService.findByCustomerAndReview(reviewer, 80));
			   model.addAttribute("reviewsFor3str", reviewService.findByCustomerAndReview(reviewer, 60));
			   model.addAttribute("reviewsFor2str", reviewService.findByCustomerAndReview(reviewer, 40));
			   model.addAttribute("reviewsFor1str", reviewService.findByCustomerAndReview(reviewer, 20));
			   model.addAttribute("reviewList", reviewService.findByCustomer(reviewer));
			   //save cookie
			   try {
					CookieUtil.saveCookie(Long.toString(reviewer.getId()), "reviewer", response, request.getCookies(), request.getContextPath());
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			   
		   }
	   }
	   return "customer.reviewer";
   }
   @RequestMapping(value="/customer-review.html", method = RequestMethod.POST)
   public String saveCustomerReview(@ModelAttribute("review") @Valid Review review, BindingResult result, 
		   @RequestParam(value = "office_id", required = true) String officeId,
		   @RequestParam(value = "redirect", required = false) String redirect,
		    SessionStatus status,
			HttpServletRequest request, Model model) {
	   Customer customer = (Customer)request.getSession().getAttribute(KsrConstants.SESSION_SCOPED_CUSTOMER);
	   if (review.getId()>0){ //update review
		   reviewService.update(review);
	   } else { //create a new review
		   Office office = officeService.findById(new Long(officeId));
		   if (review!=null&&office!=null&&customer!=null){
			   review.setCustomer(customer);
			   review.setDateUpdated(new Date());
			   review.setDateCreated(new Date());
			   review.setOffice(office);
			   reviewService.save(review);
		   }
	   }
	   if ("reviewer".equals(redirect)){
		   return "redirect:/reviewer.html?id="+customer.getId();
	   } else {
		   return "redirect:/office.html?id="+officeId;
	   }
   }
}

